/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.baicaix.chat;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import org.baicaix.chat.views.LoginDialog;
import org.baicaix.chat.views.MainFrameMediator;

/**
 * 
 * @author baicaix
 */
public class MainFrame extends JFrame {

    private ChatMainFacade facade;

    public MainFrame() {
        jbInit();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800, 600);

        facade = ChatMainFacade.getInstance();

        facade.registerMediator(new MainFrameMediator(this.toString(), this));
        
        createLoginDialog();

        facade.startUp();
    }

    private void createLoginDialog() {
        LoginDialog loginDialog = new LoginDialog(this);
        loginDialog.setLocationRelativeTo(this);
        loginDialog.setVisible(true);
    }

    private JMenuBar menuBar = null;
    private JDesktopPane desktop;
    private JTextArea logArea;

    /**
     * 初始化主界面
     */
    private void jbInit() {
        menuBar = new JMenuBar();
        menuBar.add(this.createMenu());
        this.setJMenuBar(menuBar);
        desktop = new JDesktopPane();
        logArea = new JTextArea();
        this.add(desktop, "Center");
        JScrollPane pan = new JScrollPane(logArea);
        pan.setBorder(BorderFactory.createTitledBorder("日志"));
        pan.setPreferredSize(new Dimension(800, 100));
        this.add(pan, "South");
    }

    private JMenu createMenu() {
        JMenu menu = new JMenu("File");
        JMenuItem menuItem = new JMenuItem();
        MenuItemAction action = new MenuItemAction();
        menuItem.setText("Frame01");
        menuItem.setActionCommand(ChatConstants.SHOW_FRAME_01);
        menuItem.addActionListener(action);
        menu.add(menuItem);
        menuItem = new JMenuItem();
        menuItem.setText("Frame02");
        menuItem.setActionCommand(ChatConstants.SHOW_FRAME_02);
        menuItem.addActionListener(action);
        menu.add(menuItem);
        menuItem = new JMenuItem();
        menuItem.setText("Exit");
        menuItem.setActionCommand(ChatConstants.EXIT);
        menuItem.addActionListener(action);
        menu.add(menuItem);
        return menu;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainFrame();
            }
        });
    }

    public JDesktopPane getDesktop() {
        return desktop;
    }

    private class MenuItemAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            System.out.println("action command = " + e.getActionCommand());
            String command = e.getActionCommand();
            facade.sendNotification(command, e.getSource(), null);
        }
    }

    public void showLog(String msg) {
        this.logArea.append(msg+"\n");
    }
}
